/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZTag;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.soap.admin.message.CreateSystemRetentionPolicyRequest;
/*     */ import com.zimbra.soap.admin.message.CreateSystemRetentionPolicyResponse;
/*     */ import com.zimbra.soap.admin.message.DeleteSystemRetentionPolicyRequest;
/*     */ import com.zimbra.soap.admin.message.DeleteSystemRetentionPolicyResponse;
/*     */ import com.zimbra.soap.admin.message.ModifySystemRetentionPolicyRequest;
/*     */ import com.zimbra.soap.admin.message.ModifySystemRetentionPolicyResponse;
/*     */ import com.zimbra.soap.mail.message.FolderActionRequest;
/*     */ import com.zimbra.soap.mail.message.FolderActionResponse;
/*     */ import com.zimbra.soap.mail.message.TagActionRequest;
/*     */ import com.zimbra.soap.mail.message.TagActionResponse;
/*     */ import com.zimbra.soap.mail.type.FolderActionResult;
/*     */ import com.zimbra.soap.mail.type.FolderActionSelector;
/*     */ import com.zimbra.soap.mail.type.Policy;
/*     */ import com.zimbra.soap.mail.type.Policy.Type;
/*     */ import com.zimbra.soap.mail.type.RetentionPolicy;
/*     */ import com.zimbra.soap.mail.type.TagActionInfo;
/*     */ import com.zimbra.soap.mail.type.TagActionSelector;
/*     */ import java.util.Arrays;
/*     */ import java.util.List;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestPurge
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*  48 */   private static final String NAME_PREFIX = TestPurge.class.getSimpleName();
/*     */   private String originalSystemPolicy;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  54 */     cleanUp();
/*  55 */     this.originalSystemPolicy = Provisioning.getInstance().getConfig().getMailPurgeSystemPolicy();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testFolderRetentionPolicy()
/*     */     throws Exception
/*     */   {
/*  63 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  64 */     ZFolder folder = TestUtil.createFolder(mbox, "/" + NAME_PREFIX + "-testFolderRetentionPolicy");
/*     */     
/*     */ 
/*  67 */     FolderActionSelector action = new FolderActionSelector(folder.getId(), "retentionpolicy");
/*  68 */     RetentionPolicy rp = new RetentionPolicy(Arrays.asList(new Policy[] { Policy.newUserPolicy("30d") }), null);
/*  69 */     action.setRetentionPolicy(rp);
/*  70 */     FolderActionRequest req = new FolderActionRequest(action);
/*     */     
/*  72 */     FolderActionResponse res = (FolderActionResponse)mbox.invokeJaxb(req);
/*  73 */     assertEquals("retentionpolicy", res.getAction().getOperation());
/*  74 */     assertEquals(folder.getId(), res.getAction().getId());
/*     */     
/*     */ 
/*  77 */     folder = mbox.getFolderById(folder.getId());
/*  78 */     rp = folder.getRetentionPolicy();
/*  79 */     assertEquals(1, rp.getKeepPolicy().size());
/*  80 */     assertEquals(0, rp.getPurgePolicy().size());
/*  81 */     Policy p = (Policy)rp.getKeepPolicy().get(0);
/*  82 */     assertEquals(Policy.Type.USER, p.getType());
/*  83 */     assertEquals("30d", p.getLifetime());
/*     */     
/*     */ 
/*  86 */     action = new FolderActionSelector(folder.getId(), "retentionpolicy");
/*  87 */     rp = new RetentionPolicy(null, Arrays.asList(new Policy[] { Policy.newUserPolicy("45d") }));
/*  88 */     action.setRetentionPolicy(rp);
/*  89 */     req = new FolderActionRequest(action);
/*     */     
/*  91 */     res = (FolderActionResponse)mbox.invokeJaxb(req);
/*  92 */     assertEquals("retentionpolicy", res.getAction().getOperation());
/*  93 */     assertEquals(folder.getId(), res.getAction().getId());
/*     */     
/*     */ 
/*  96 */     folder = mbox.getFolderById(folder.getId());
/*  97 */     rp = folder.getRetentionPolicy();
/*  98 */     assertEquals(0, rp.getKeepPolicy().size());
/*  99 */     assertEquals(1, rp.getPurgePolicy().size());
/* 100 */     p = (Policy)rp.getPurgePolicy().get(0);
/* 101 */     assertEquals(Policy.Type.USER, p.getType());
/* 102 */     assertEquals("45d", p.getLifetime());
/*     */     
/*     */ 
/* 105 */     mbox = TestUtil.getZMailbox("user1");
/* 106 */     folder = mbox.getFolderById(folder.getId());
/* 107 */     assertTrue(folder.getRetentionPolicy().isSet());
/*     */   }
/*     */   
/*     */   public void testTagRetentionPolicy() throws Exception {
/* 111 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 112 */     ZTag tag = mbox.createTag(NAME_PREFIX + "-testTagRetentionPolicy", null);
/*     */     
/*     */ 
/* 115 */     TagActionSelector action = new TagActionSelector(tag.getId(), "retentionpolicy");
/* 116 */     RetentionPolicy rp = new RetentionPolicy(Arrays.asList(new Policy[] { Policy.newUserPolicy("30d") }), null);
/* 117 */     action.setRetentionPolicy(rp);
/* 118 */     TagActionRequest req = new TagActionRequest(action);
/*     */     
/* 120 */     TagActionResponse res = (TagActionResponse)mbox.invokeJaxb(req);
/* 121 */     assertEquals("retentionpolicy", res.getAction().getOperation());
/* 122 */     assertEquals(tag.getId(), res.getAction().getSuccesses());
/*     */     
/*     */ 
/* 125 */     tag = mbox.getTagById(tag.getId());
/* 126 */     rp = tag.getRetentionPolicy();
/* 127 */     assertEquals(1, rp.getKeepPolicy().size());
/* 128 */     assertEquals(0, rp.getPurgePolicy().size());
/* 129 */     Policy p = (Policy)rp.getKeepPolicy().get(0);
/* 130 */     assertEquals(Policy.Type.USER, p.getType());
/* 131 */     assertEquals("30d", p.getLifetime());
/*     */     
/*     */ 
/* 134 */     action = new TagActionSelector(tag.getId(), "retentionpolicy");
/* 135 */     rp = new RetentionPolicy(null, Arrays.asList(new Policy[] { Policy.newUserPolicy("45d") }));
/* 136 */     action.setRetentionPolicy(rp);
/* 137 */     req = new TagActionRequest(action);
/*     */     
/* 139 */     res = (TagActionResponse)mbox.invokeJaxb(req);
/* 140 */     assertEquals("retentionpolicy", res.getAction().getOperation());
/* 141 */     assertEquals(tag.getId(), res.getAction().getSuccesses());
/*     */     
/*     */ 
/* 144 */     tag = mbox.getTagById(tag.getId());
/* 145 */     rp = tag.getRetentionPolicy();
/* 146 */     assertEquals(0, rp.getKeepPolicy().size());
/* 147 */     assertEquals(1, rp.getPurgePolicy().size());
/* 148 */     p = (Policy)rp.getPurgePolicy().get(0);
/* 149 */     assertEquals(Policy.Type.USER, p.getType());
/* 150 */     assertEquals("45d", p.getLifetime());
/*     */     
/*     */ 
/* 153 */     mbox = TestUtil.getZMailbox("user1");
/* 154 */     tag = mbox.getTagById(tag.getId());
/* 155 */     assertTrue(tag.getRetentionPolicy().isSet());
/*     */   }
/*     */   
/*     */   public void testSystemRetentionPolicy() throws Exception {
/* 159 */     SoapProvisioning prov = TestUtil.newSoapProvisioning();
/*     */     
/*     */ 
/* 162 */     com.zimbra.soap.admin.message.GetSystemRetentionPolicyRequest getReq = new com.zimbra.soap.admin.message.GetSystemRetentionPolicyRequest();
/* 163 */     com.zimbra.soap.admin.message.GetSystemRetentionPolicyResponse getRes = (com.zimbra.soap.admin.message.GetSystemRetentionPolicyResponse)prov.invokeJaxb(getReq);
/* 164 */     RetentionPolicy rp = getRes.getRetentionPolicy();
/* 165 */     assertEquals(0, rp.getKeepPolicy().size());
/* 166 */     assertEquals(0, rp.getPurgePolicy().size());
/*     */     
/*     */ 
/* 169 */     Policy keep = Policy.newSystemPolicy("keep", "60d");
/* 170 */     CreateSystemRetentionPolicyRequest createReq = CreateSystemRetentionPolicyRequest.newKeepRequest(keep);
/* 171 */     CreateSystemRetentionPolicyResponse createRes = (CreateSystemRetentionPolicyResponse)prov.invokeJaxb(createReq);
/* 172 */     Policy p = createRes.getPolicy();
/* 173 */     assertNotNull(p.getId());
/* 174 */     assertEquals(keep.getName(), p.getName());
/* 175 */     assertEquals(keep.getLifetime(), p.getLifetime());
/* 176 */     keep = p;
/*     */     
/*     */ 
/* 179 */     Policy purge1 = Policy.newSystemPolicy("purge1", "120d");
/* 180 */     createReq = CreateSystemRetentionPolicyRequest.newPurgeRequest(purge1);
/* 181 */     createRes = (CreateSystemRetentionPolicyResponse)prov.invokeJaxb(createReq);
/* 182 */     purge1 = createRes.getPolicy();
/*     */     
/* 184 */     Policy purge2 = Policy.newSystemPolicy("purge2", "240d");
/* 185 */     createReq = CreateSystemRetentionPolicyRequest.newPurgeRequest(purge2);
/* 186 */     createRes = (CreateSystemRetentionPolicyResponse)prov.invokeJaxb(createReq);
/* 187 */     purge2 = createRes.getPolicy();
/*     */     
/*     */ 
/* 190 */     getRes = (com.zimbra.soap.admin.message.GetSystemRetentionPolicyResponse)prov.invokeJaxb(getReq);
/* 191 */     rp = getRes.getRetentionPolicy();
/* 192 */     assertEquals(1, rp.getKeepPolicy().size());
/* 193 */     assertEquals(keep, rp.getKeepPolicy().get(0));
/* 194 */     assertEquals(2, rp.getPurgePolicy().size());
/* 195 */     assertEquals(purge1, rp.getPolicyById(purge1.getId()));
/* 196 */     assertEquals(purge2, rp.getPolicyById(purge2.getId()));
/*     */     
/*     */ 
/* 199 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 200 */     com.zimbra.soap.mail.message.GetSystemRetentionPolicyResponse mailRes = (com.zimbra.soap.mail.message.GetSystemRetentionPolicyResponse)mbox.invokeJaxb(new com.zimbra.soap.mail.message.GetSystemRetentionPolicyRequest());
/*     */     
/* 202 */     assertEquals(rp, mailRes.getRetentionPolicy());
/*     */     
/*     */ 
/* 205 */     Policy modLifetime = Policy.newSystemPolicy(purge1.getId(), null, "121d");
/* 206 */     ModifySystemRetentionPolicyRequest modifyReq = new ModifySystemRetentionPolicyRequest(modLifetime);
/* 207 */     ModifySystemRetentionPolicyResponse modifyRes = (ModifySystemRetentionPolicyResponse)prov.invokeJaxb(modifyReq);
/* 208 */     Policy newPurge1 = modifyRes.getPolicy();
/* 209 */     assertEquals(Policy.newSystemPolicy(purge1.getId(), "purge1", "121d"), newPurge1);
/*     */     
/* 211 */     getRes = (com.zimbra.soap.admin.message.GetSystemRetentionPolicyResponse)prov.invokeJaxb(getReq);
/* 212 */     assertEquals(newPurge1, getRes.getRetentionPolicy().getPolicyById(newPurge1.getId()));
/*     */     
/*     */ 
/* 215 */     Policy modName = Policy.newSystemPolicy(purge1.getId(), "purge1-new", null);
/* 216 */     modifyReq = new ModifySystemRetentionPolicyRequest(modName);
/* 217 */     modifyRes = (ModifySystemRetentionPolicyResponse)prov.invokeJaxb(modifyReq);
/* 218 */     newPurge1 = modifyRes.getPolicy();
/* 219 */     assertEquals(Policy.newSystemPolicy(purge1.getId(), "purge1-new", "121d"), newPurge1);
/*     */     
/*     */ 
/* 222 */     DeleteSystemRetentionPolicyRequest deleteReq = new DeleteSystemRetentionPolicyRequest(purge1);
/*     */     
/* 224 */     DeleteSystemRetentionPolicyResponse deleteRes = (DeleteSystemRetentionPolicyResponse)prov.invokeJaxb(deleteReq);
/* 225 */     getRes = (com.zimbra.soap.admin.message.GetSystemRetentionPolicyResponse)prov.invokeJaxb(getReq);
/* 226 */     rp = getRes.getRetentionPolicy();
/* 227 */     assertEquals(1, rp.getKeepPolicy().size());
/* 228 */     assertEquals(keep, rp.getKeepPolicy().get(0));
/* 229 */     assertEquals(1, rp.getPurgePolicy().size());
/* 230 */     assertEquals(purge2, rp.getPurgePolicy().get(0));
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 235 */     cleanUp();
/* 236 */     Provisioning.getInstance().getConfig().setMailPurgeSystemPolicy(this.originalSystemPolicy);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/* 240 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 245 */     TestUtil.cliSetup();
/* 246 */     TestUtil.runTest(TestPurge.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestPurge.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */